﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VoteUphold;
using System.Threading;

namespace SendMessage
{
    class Program
    {
        static string RootUrl = "https://feixin.10086.cn/account/register/";
        static string SendMessagePostUrl = "https://feixin.10086.cn/account/RegisterLv3smsCodeAjax";
        static void Main(string[] args)
        {
            Testt();

            string mobiles = System.Configuration.ConfigurationManager.AppSettings["Mobile"];
            if (!string.IsNullOrEmpty(mobiles))
            {
                Console.WriteLine("你配置的手机号码为:" + mobiles);
                Console.WriteLine("确定继续(y/n):");

                string input = Console.ReadLine();
                if (!string.IsNullOrEmpty(input) && input.ToLower() == "y")
                {
                    Console.WriteLine("开始执行:");
                    string[] mobilesAttry = mobiles.Split(',');
                    while (true)
                    {
                        try
                        {
                            SendMes(mobilesAttry);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        int sleep = new Random().Next(62, 70);
                        Console.WriteLine(string.Format("程序休息{0}秒", sleep));
                        Thread t = new Thread(new ThreadStart(delegate() {
                            for (int i = 0; i < sleep; i++)
                            {
                                Console.Write(i);
                            }
                            Console.WriteLine();
                        }));

                        Thread.Sleep(sleep * 1000);
                    }
                }
                else
                {
                    Console.WriteLine("输入错误..结束..");
                }
            }
            else
            {
                Console.WriteLine("你未配置手机号码.结束..");

            }
            Console.ReadLine();
        }


        private static void Testt()
        {
            string url = "http://localhost:8091/Portal/GmccADC/ADCECWapPortal/ADCECWapSite/DL100Index.aspx";
            HttpClient httpClient = new HttpClient(new Uri(url));
            httpClient.Headers.Add("X-Forwarded-For", GetRandIp());
            string content= httpClient.DownloadString(url);
        }

        private static void SendMes(string[] mobilesArry)
        {
            for (int i = 0; i < mobilesArry.Length; i++)
            {
                HttpClient httpClient = new HttpClient(new Uri(RootUrl));
                httpClient.Headers.Add("X-Forwarded-For", GetRandIp());
                string result = httpClient.PostData(SendMessagePostUrl, "mobileno=" + mobilesArry[i], Encoding.UTF8, Encoding.UTF8);
                Console.WriteLine(string.Format("向{0}发送了..返回结果:{1}", mobilesArry[i], result));
            }
        }


        private static string GetRandIp()
        {
            Random rd = new Random();
            string ip = string.Format("{0}.{1}.{2}.{3}", rd.Next(1, 255), rd.Next(1, 255), rd.Next(1, 255), rd.Next(1, 255));
            return ip;
        }
    }
}
